package com.quanyan.place.service.impl;

import com.quanyan.api.APIResponse;
import com.quanyan.common.Plugin.Page;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.common.constants.Constants;
import com.quanyan.place.constants.PlaceConstants;
import com.quanyan.place.entity.*;
import com.quanyan.place.entity.vo.PlaceFinanceAuditVo;
import com.quanyan.place.entity.vo.ReqFinanceAuditSearch;
import com.quanyan.place.mapper.TbBizPlaceFinanceAuditMapper;
import com.quanyan.place.mapper.TbBizPlaceMemberOpHistoryMapper;
import com.quanyan.place.mapper.TbBizPlaceOrderMapper;
import com.quanyan.place.mapper.VFinanceAuditMapper;
import com.quanyan.place.service.FinanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * Created by admin on 2017/5/5.
 */
@Service("financeService")
public class FinanceServiceImpl implements FinanceService {

    @Autowired
    TbBizPlaceFinanceAuditMapper tbBizPlaceFinanceAuditMapper;

    @Autowired
    TbBizPlaceOrderMapper tbBizPlaceOrderMapper;

    @Autowired
    VFinanceAuditMapper financeAuditMapper;

    @Autowired
    TbBizPlaceMemberOpHistoryMapper memberOpHistoryMapper;

    @Override
    public int countPlaceFinanceAudit(ReqFinanceAuditSearch reqSearch) {
        int totalNum = 0;
        if (null != reqSearch){
            TbBizPlaceFinanceAuditExample tbBizPlaceFinanceAuditExample = new TbBizPlaceFinanceAuditExample();
            TbBizPlaceFinanceAuditExample.Criteria criteriaParameter = tbBizPlaceFinanceAuditExample.createCriteria();
            if (null != reqSearch.getBusinessType()){
                criteriaParameter.andBusinessTypeEqualTo(reqSearch.getBusinessType());
            }
            totalNum = tbBizPlaceFinanceAuditMapper.countByExample(tbBizPlaceFinanceAuditExample);
        }
        return totalNum;
    }

    /**
     * 通过父订单Id查询子订单
     * @param parenetId
     * @return
     */
    public List<TbBizPlaceOrder> getOrdersByPrivateId(Integer parenetId) {
        TbBizPlaceOrderExample tbBizPlaceOrderExample = new TbBizPlaceOrderExample();
        TbBizPlaceOrderExample.Criteria tbBizPlaceOrderCriteria = tbBizPlaceOrderExample.createCriteria();
        tbBizPlaceOrderCriteria.andParentIdEqualTo(parenetId);
        tbBizPlaceOrderCriteria.andUseTypeEqualTo(PlaceConstants.ORDER_USE_BLOCK_BOOKING); // 固定场
        List<TbBizPlaceOrder> tbBizPlaceOrdersList = tbBizPlaceOrderMapper.selectByExample(tbBizPlaceOrderExample);
        return tbBizPlaceOrdersList;
    }

    @Override
    public TbBizPlaceOrder getOrdersByOrderNo(String placeOrderId) {
        TbBizPlaceOrderExample tbBizPlaceOrderExample = new TbBizPlaceOrderExample();
        TbBizPlaceOrderExample.Criteria tbBizPlaceOrderCriteria = tbBizPlaceOrderExample.createCriteria();
        tbBizPlaceOrderCriteria.andPlaceOrderIdEqualTo(placeOrderId);
        List<TbBizPlaceOrder> tbBizPlaceOrdersList = tbBizPlaceOrderMapper.selectByExample(tbBizPlaceOrderExample);
        if (null != tbBizPlaceOrdersList && tbBizPlaceOrdersList.size() > 0){
            return tbBizPlaceOrdersList.get(0);
        } else {
            return null;
        }
    }

    @Override
    public TbBizPlaceMemberOpHistory getMemberOpHistoryByOrderNo(String placeOrderId) {
        TbBizPlaceMemberOpHistoryExample opHistoryhExample = new TbBizPlaceMemberOpHistoryExample();
        TbBizPlaceMemberOpHistoryExample.Criteria opHistoryCriteria = opHistoryhExample.createCriteria();
        opHistoryCriteria.andOrderNoEqualTo(placeOrderId);
        List<TbBizPlaceMemberOpHistory> opHistoryList = memberOpHistoryMapper.selectByExample(opHistoryhExample);
        if (null != opHistoryList && opHistoryList.size() > 0){
            return opHistoryList.get(0);
        } else {
            return null;
        }
    }

    /**
     * 获取审核列表
     * @param reqSearch
     * @return
     */
    @Override
    public List<TbBizPlaceFinanceAudit> getPlaceFinanceAuditList(ReqFinanceAuditSearch reqSearch,
                                                                 int pageSize,
                                                                 int pageNum,
                                                                 int totalNum) {
        List<TbBizPlaceFinanceAudit> result = new ArrayList<TbBizPlaceFinanceAudit>();
        if (null == reqSearch){
            return result;
        }
        TbBizPlaceFinanceAuditExample tbBizPlaceFinanceAuditExample = new TbBizPlaceFinanceAuditExample();
        TbBizPlaceFinanceAuditExample.Criteria criteriaParameter = tbBizPlaceFinanceAuditExample.createCriteria();
        if (null != reqSearch.getBusinessType()){
            criteriaParameter.andBusinessTypeEqualTo(reqSearch.getBusinessType());
        }
        tbBizPlaceFinanceAuditExample.setOrderByClause(" id desc ");
        tbBizPlaceFinanceAuditExample.setPage(new Page(pageNum * pageSize, pageSize, totalNum));
        result = tbBizPlaceFinanceAuditMapper.selectByExample(tbBizPlaceFinanceAuditExample);

        return result;
    }

    /**
     * 获取审核列表
     * @param reqSearch
     * @return
     */
    @Override
    public List<PlaceFinanceAuditVo> getPlaceFinanceAuditList(ReqFinanceAuditSearch reqSearch) {
        if (null == reqSearch){
            return null;
        }
        return financeAuditMapper.selectFinanceAuditInfo(reqSearch);
    }

    /**
     * 插入待审核记录
     * @param tbBizPlaceFinanceAudit
     * @return
     */
    @Override
    public APIResponse insertToAuditRecord(TbBizPlaceFinanceAudit tbBizPlaceFinanceAudit) {

        tbBizPlaceFinanceAudit.setSubjectTime(Calendar.getInstance().getTime());
        tbBizPlaceFinanceAudit.setCreateTime(Calendar.getInstance().getTime());
        this.tbBizPlaceFinanceAuditMapper.insert(tbBizPlaceFinanceAudit);
        return APIResponse.returnSuccess();
    }


    /**
     * 创建对公转账订单待审核记录
     * @param placeFinanceAuditVo
     * @return
     */
    @Override
    public void createAuditRecord(PlaceFinanceAuditVo placeFinanceAuditVo) {
        TbBizPlaceFinanceAudit tbBizPlaceFinanceAudit = new TbBizPlaceFinanceAudit();
        tbBizPlaceFinanceAudit.setBusinessType(placeFinanceAuditVo.getBusinessType()); // 外部业务区分业务类型
        tbBizPlaceFinanceAudit.setSubType(placeFinanceAuditVo.getSubType());
        tbBizPlaceFinanceAudit.setBusinessNo(placeFinanceAuditVo.getBusinessNo());
        tbBizPlaceFinanceAudit.setPlaceId(placeFinanceAuditVo.getPlaceId());
        tbBizPlaceFinanceAudit.setSubjectUid(placeFinanceAuditVo.getSubjectUid()); // 提交人
        tbBizPlaceFinanceAudit.setSubjectUserName(placeFinanceAuditVo.getSubjectUserName()); // 提交人
        tbBizPlaceFinanceAudit.setFirstAuditStatus(PlaceFinanceAuditVo.STATUS_TODO_AUDIT); // 待审核
        //tbBizPlaceFinanceAudit.setExtendInfo1(placeFinanceAuditVo.getMemberCardNo());
        if (!StringUtils.isEmpty(placeFinanceAuditVo.getParentId())){
            tbBizPlaceFinanceAudit.setExtendInfo2(placeFinanceAuditVo.getParentId().toString());//固定场订单id
        }
        this.insertToAuditRecord(tbBizPlaceFinanceAudit);
    }

    /**
     * 根据ID获取审核记录
     *
     * @param id
     * @return
     */
    public TbBizPlaceFinanceAudit getPlaceFinanceAudit(Integer id) {
        return tbBizPlaceFinanceAuditMapper.selectByPrimaryKey(id);
    }

    /**
     * 更新审核记录
     * @param tbBizPlaceFinanceAudit
     */
    @Override
    public void updateTbBizPlaceFinanceAudit(TbBizPlaceFinanceAudit tbBizPlaceFinanceAudit) {
        tbBizPlaceFinanceAuditMapper.updateByPrimaryKeySelective(tbBizPlaceFinanceAudit);
    }


}
